<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="本科学校" prop="schoolId">
        <el-select v-model="queryParams.schoolId" placeholder="本科学校">
            <el-option
                v-for="school in bSchoolOptions"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="本科学院" prop="collegeId">
        <el-select v-model="queryParams.collegeId" placeholder="本科学院">
            <el-option
                v-for="college in bCollegeOptions"
                :key="college.collegeId"
                :label="college.collegeName"
                :value="college.collegeId"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="本科专业" prop="majorId">
        <el-select v-model="queryParams.majorId" placeholder="本科专业">
            <el-option
                v-for="major in bMajorOptions"
                :key="major.majorId"
                :label="major.majorName"
                :value="major.majorId"
              />
          </el-select>
      </el-form-item>
      <!--
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录取分数" prop="adScore">
        <el-input
          v-model="queryParams.adScore"
          placeholder="请输入录取分数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="招生计划" prop="adNum">
        <el-input
          v-model="queryParams.adNum"
          placeholder="请输入招生计划(人数)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分数线" prop="scoreLine">
        <el-input
          v-model="queryParams.scoreLine"
          placeholder="请输入分数控制线"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
          </el-select>
      </el-form-item>
      -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:brecruitPlan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:brecruitPlan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:brecruitPlan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:brecruitPlan:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recruitPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="计划ID" align="center" prop="planId" />-->
      <el-table-column label="本科学校" align="center" prop="schoolName" />
      <el-table-column label="本科学院" align="center" prop="collegeName" />
      <el-table-column label="本科专业" align="center" prop="majorName" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="录取分数" align="center" prop="adScore" />
      <el-table-column label="招生计划(人数)" align="center" prop="adNum" />
      <el-table-column label="分数控制线" align="center" prop="scoreLine" />
      <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />
      <!--<el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:brecruitPlan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:brecruitPlan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改本科学校招生计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="本科学校" prop="schoolId">
        <el-select v-model="form.schoolId" placeholder="本科学校">
            <el-option
                v-for="school in bSchoolOptions"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="本科学院" prop="collegeId">
        <el-select v-model="form.collegeId" placeholder="本科学院">
            <el-option
                v-for="college in bCollegeOptions"
                :key="college.collegeId"
                :label="college.collegeName"
                :value="college.collegeId"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="本科专业" prop="majorId">
        <el-select v-model="form.majorId" placeholder="本科专业">
            <el-option
                v-for="major in bMajorOptions"
                :key="major.majorId"
                :label="major.majorName"
                :value="major.majorId"
              />
          </el-select>
      </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="录取分数" prop="adScore">
          <el-input v-model="form.adScore" placeholder="请输入录取分数" />
        </el-form-item>
        <el-form-item label="招生计划(人数)" prop="adNum">
          <el-input v-model="form.adNum" placeholder="请输入招生计划(人数)" />
        </el-form-item>
        <el-form-item label="分数控制线" prop="scoreLine">
          <el-input v-model="form.scoreLine" placeholder="请输入分数控制线" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecruitPlan, getRecruitPlan, delRecruitPlan, addRecruitPlan, updateRecruitPlan, exportRecruitPlan } from "@/api/edu/brecruitPlan";
import { listSchool } from "@/api/edu/bSchools";
import { getBMajorsByCollegeId } from "@/api/edu/bMajors";
import { getBCollegesBySchoolId } from "@/api/edu/bColleges";

export default {
  name: "RecruitPlan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 本科学校招生计划表格数据
      recruitPlanList: [],
      bSchoolOptions: [],
      bCollegeOptions: [],
      bMajorOptions: [],
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        collegeId: null,
        majorId: null,
        year: null,
        adScore: null,
        adNum: null,
        scoreLine: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        schoolId: [
          { required: true, message: "本科学校不能为空", trigger: "blur" }
        ],
        collegeId: [
          { required: true, message: "本科学院不能为空", trigger: "blur" }
        ],
        majorId: [
          { required: true, message: "本科专业不能为空", trigger: "blur" }
        ],
        year: [
          { required: true, message: "年份不能为空", trigger: "blur" }
        ],
        adScore: [
          { required: true, message: "录取分数不能为空", trigger: "blur" }
        ],
        adNum: [
          { required: true, message: "招生计划(人数)不能为空", trigger: "blur" }
        ],
        scoreLine: [
          { required: true, message: "分数控制线不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryBSchool()
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });

    const schoolId = this.$route.params && this.$route.params.schoolId;
    if(undefined !== schoolId && null !== schoolId && "" !== schoolId) {
      this.queryParams.schoolId = parseInt(schoolId)
    }
    this.getList();
  },
  watch: {
    'queryParams.schoolId': 'selectBColleges',
    'form.schoolId': 'selectBColleges',
    'queryParams.collegeId': 'selectBMajors',
    'form.collegeId': 'selectBMajors',
  },
  methods: {
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    queryBSchool() {
      listSchool().then(response => {
        this.bSchoolOptions = response.data
      })
    },
    selectBColleges() {
      if((this.queryParams.schoolId === undefined || this.queryParams.schoolId === null)
        && (this.form.schoolId === undefined || this.form.schoolId === null)) {
        this.$message({
            type: 'warning',
            message: '请选择本科学校'
          })
          return
      }

      let param = this.queryParams.schoolId
      if(this.queryParams.schoolId === undefined || this.queryParams.schoolId === null) {
        param = this.form.schoolId
      }
      
      getBCollegesBySchoolId(parseInt(param)).then(response => {
        this.bCollegeOptions = response.data
      })
    },
    selectBMajors() {
      if((this.queryParams.collegeId === undefined || this.queryParams.collegeId === null)
        && (this.form.collegeId === undefined || this.form.collegeId === null)) {
        this.$message({
            type: 'warning',
            message: '请选择本科学院'
          })
          return
      }

      let param = this.queryParams.collegeId
      if(this.queryParams.collegeId === undefined || this.queryParams.collegeId === null) {
        param = this.form.collegeId
      }
      
      getBMajorsByCollegeId(parseInt(param)).then(response => {
        this.bMajorOptions = response.data
      })
    },
    /** 查询本科学校招生计划列表 */
    getList() {
      this.loading = true;
      listRecruitPlan(this.queryParams).then(response => {
        this.recruitPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        schoolId: null,
        collegeId: null,
        majorId: null,
        schoolName: null,
        collegeName: null,
        majorName: null,
        year: null,
        adScore: null,
        adNum: null,
        scoreLine: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加本科学校招生计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getRecruitPlan(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改本科学校招生计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updateRecruitPlan(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecruitPlan(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$confirm('是否确认删除本科学校招生计划编号为"' + planIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecruitPlan(planIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有本科学校招生计划数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecruitPlan(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
